package com.example.lawyer.netty.handler;

import com.example.lawyer.netty.NettyConfig;
import com.example.lawyer.netty.activityhandler.ActivityMessageCallback;
import com.example.lawyer.service.NettyService;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

public class NettyMessageHandler extends ChannelInboundMessageHandlerAdapter<String> {

    public NettyService nettyService;

    public NettyMessageHandler(NettyService nettyService, Class<?>... acceptedMsgTypes) {
        super(acceptedMsgTypes);
        this.nettyService = nettyService;
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {

        System.out.println("收到消息");
        nettyService.ReceiveMessage(msg);

    }


    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
        System.out.println("收到消息结束");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        System.out.println("客户端连接断开");
    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("建立连接");


    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        System.out.println("程序出现异常");
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("用户心调");
        System.out.println(ctx.channel().id());
        System.out.println(ctx.channel().isActive());


        super.userEventTriggered(ctx, evt);


    }
}
